const { add, update, del, find, findpage } = require("../model/solveModel")
const moment = require('moment')//格式化时间的插件
const jwt = require("jsonwebtoken");

//添加事务
const adder = async function (req, res, next) {
    //查看event（事件内容）是否传入
    if (!req.body.event) {
        res.send({ state: false, status: 0, msg: "event必填" })
        return;
    }
    //创建传入对象
    let authorization = req.body.authorization || req.headers.authorization || req.query.authorization;
    let secrect = "How_are_you";
    jwt.verify(authorization, secrect, function (err, decode) {
        let cTime = moment().format("YYYY/MM/DD HH:mm:ss a")
        let data = {
            cTime,
            admin: decode.name,
            event: req.body.event,
            solveState: -1//未处理
        }
        console.log(data);
        //开始创建
        add(data).then(r => {
            res.send({ state: true, status: 1, msg: "添加成功" })
        }).catch(err => {
            res.send({ state: false, status: 0, msg: "添加失败" })
        })
    })
}

//修改状态
const solver = async function (req, res, next) {
    //查看_id是否传入
    if (!req.body._id) {
        res.send({ state: false, status: 0, msg: "_id必填" })
        return;
    }
    let query = { _id: req.body._id }
    let data = {}
    data.solveTime = moment().format("YYYY/MM/DD HH:mm:ss a")
    if (req.body.Remarks) { data.Remarks = req.body.Remarks }
    data.solveState = 1;
    update(query, data).then(r => {
        //修改成功
        res.send({ state: true, status: 1, msg: "修改成功" })
    }).catch(err => {
        //修改失败
        res.send({ state: false, status: 0, msg: "修改失败" })
    })
}

//删除事务(撤销事务)
const dlter = async function (req, res) {
    //查看_id是否传入
    if (!req.body._id) {
        res.send({ state: false, status: 0, msg: "_id必填" })
        return;
    }
    //开始删除
    del({ _id: req.body._id }).then(r => {
        res.send({ state: true, status: 1, msg: "撤销事务成功" })
    }).catch(err => {
        res.send({ state: false, status: 0, msg: "撤销事务失败" })
    })
}

//查询所有事务  可以查看总数据量
const getaller = async function (req, res) {
    let authorization = req.body.authorization || req.headers.authorization || req.query.authorization;
    let secrect = "How_are_you";
    jwt.verify(authorization, secrect, function (err, decode) {
        let query = {}
        if (decode.name !== "root") {
            query.admin = decode.name
        }
        find(query).then(r => {
            res.send({ state: true, status: 1, data: r })
        })
    })
}

//查询事务  分页
const getpageer = async function (req, res) {
    let page = req.query['page'] || 1;
    let count = req.query['count'] || 5;
    let counts = {
        skip: (page - 1) * count,
        limit: count - 0
    }
    let authorization = req.body.authorization || req.headers.authorization || req.query.authorization;
    let secrect = "How_are_you";
    jwt.verify(authorization, secrect, function (err, decode) {
        let query = {}
        if (decode.name !== "root") {
            query.admin = decode.name
        }
        findpage(query,counts).then(r => {
            res.send({ state: true, status: 1, data: r })
        })
    })
}

//加急
const faster = async function (req, res, next) {
    //查看_id是否传入
    if (!req.body._id) {
        res.send({ state: false, status: 0, msg: "_id必填" })
        return;
    }
    let query = { _id: req.body._id }
    let data = {}
    data.cTime = moment().format("YYYY/MM/DD HH:mm:ss a")
    update(query, data).then(r => {
        //修改成功
        res.send({ state: true, status: 1, msg: "修改成功" })
    }).catch(err => {
        //修改失败
        res.send({ state: false, status: 0, msg: "修改失败" })
    })
}

module.exports = {
    adder, solver, dlter, getaller, getpageer,faster
}